package com.xiaobawang.xjsyy_idea_plugin.util;

import com.xiaobawang.xjsyy_idea_plugin.panel.dev_process.view.DataFetcher;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.Vector;

@Data
@EqualsAndHashCode(callSuper = false)
public class PagingTable {
    private JPanel mainPanel;
    private JTable table;
    private DefaultTableModel model;
    private JScrollPane scrollPane;
    private int currentPage = 1;
    private int rowsPerPage = 10; // 默认每页行数
    private JTextField pageNumberField;
    private JComboBox<Integer> rowsPerPageComboBox;
    private Vector<String> columnNames;
    private DataFetcher dataFetcher;

    public PagingTable(Vector<String> columnNames, DataFetcher dataFetcher) {
        this.columnNames = columnNames;
        this.dataFetcher = dataFetcher;
        model = new DefaultTableModel(columnNames, 0);
        table = new JTable(model);
        table.setEnabled(false);
        scrollPane = new JScrollPane(table);
        mainPanel = new JPanel();
        setupPagingControls();
        initData();
    }

    private void updateTableForPage(int pageNum) {

        // 从数据服务中获取对应页的数据
        Vector<Vector<Object>> tempData = this.dataFetcher.dataFetch(pageNum, rowsPerPage);

        // 清空模型并添加新数据
        model.setRowCount(0);
        for (Vector<Object> row : tempData) {
            model.addRow(row);
        }

        // 更新界面
        table.revalidate();
        table.repaint();
        currentPage = pageNum;
        pageNumberField.setText(Integer.toString(pageNum));
    }

    public void initData() {
        rowsPerPage = 10;
        currentPage = 1; // 重置当前页为第一页
        updateTableForPage(currentPage);
    }

    public void setupPagingControls() {
        // 分页控制按钮
        JButton prevButton = new JButton("< Previous");
        prevButton.addActionListener(e -> {
            if (currentPage > 1) {
                currentPage--;
                updateTableForPage(currentPage);
            }
        });

        JButton nextButton = new JButton("Next >");
        nextButton.addActionListener(e -> {
            currentPage++;
            updateTableForPage(currentPage);
        });

        // 跳转到指定页的输入框
        pageNumberField = new JTextField(Integer.toString(currentPage), 3);
        pageNumberField.addActionListener(e -> {
            try {
                currentPage = Integer.parseInt(pageNumberField.getText());
                updateTableForPage(currentPage);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "Please enter a valid number.");
            }
        });

        // 更改每页显示数量的下拉框
        Integer[] options = {5, 10, 20, 50};
        rowsPerPageComboBox = new JComboBox<>(options);
        rowsPerPageComboBox.setSelectedIndex(1);
        rowsPerPageComboBox.addActionListener(e -> {
            rowsPerPage = (Integer) rowsPerPageComboBox.getSelectedItem();
            currentPage = 1; // 重置当前页为第一页
            updateTableForPage(currentPage);
        });

        // 添加到面板
        JPanel controlPanel = new JPanel();
        controlPanel.add(prevButton);
        controlPanel.add(pageNumberField);
        controlPanel.add(nextButton);
        controlPanel.add(new JLabel("Rows per page:"));
        controlPanel.add(rowsPerPageComboBox);

        mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(scrollPane, BorderLayout.CENTER);
        mainPanel.add(controlPanel, BorderLayout.SOUTH);
    }
}